package com.mateng.treasurechest.utils

import com.google.gson.Gson
import com.mateng.treasurechest.utils.GsonUtils.Single.gson

/**
 * Created by Administrator on 2018/2/2.
 */

class GsonUtils private constructor() {

    companion object {
        val instance: GsonUtils
            get() {
                return Single.gsonUtils
            }
    }

    private object Single {
        val gsonUtils = GsonUtils()
        val gson = Gson()
    }

    // object 转成 json
    fun objectToJson(any: Any): String {
        return gson.toJson(any)
    }

    // string 转成 Any
    fun <T> jsonToAny(json: String, cls: Class<T>): T {
        return gson.fromJson(json, cls)
    }

}
